$(function() {
	$(".context").height($(window).height() - 100);
	$("#index_menu").tree({
		onClick : function(node, bb) {
			$("#index_center").panel({ title:node.path });
			$(".easyui-simplegrid").simplegrid({
				url: "system/Menu/get",
				data : {pid : node.id}
			});
		},
		onContextMenu: function(e, node){
			e.preventDefault();
			$('#index_menu').tree('select', node.target);
			$('#contextmenu').menu('show', {
				left: e.pageX,
				top: e.pageY
			});
		}
	});
	$.ajax({
		url: 'themes/icon.css',
		success: function(_data) {
			var data = _data.split("\n");
			for (var i=0; i<data.length; i++) {
				var icons = data[i].split(" ")[0].substring(1);
				if (i%20 == 0) {
					$("#icons-talbe").append("<tr></tr>");
				}
				var td = $("<td class=\"" + icons + "\" style=\"cursor: pointer;\">&nbsp;</td>").click(function() {
					$("#menuiconcls").val($(this).attr("class")).attr("class", $(this).attr("class"));
					$('#icons-div').window('close');
				});
				$("#icons-talbe").find("tr:last").append(td);
			}
		}
	});
});

/**
 * 添加子菜单
 */
function btn_add(id) {
	var now = $("#index_menu").tree("getSelected");
	var pid;
	if (id == 'tree') {
		var parent = $("#index_menu").tree("getParent", now.target);
		pid = (parent != null ? parent.id || "" : "");
	} else {
		pid = (now != null ? now.id || "" : "");
	}
	$("#menuinfo-button").unbind();
	$("#menuinfo-button").click(exec_add);
	$("#menuiconcls").attr("class", "");
	$("#menuinfo-form").find("input[name='menuInfo.pid']").val(pid);
	$("#menuinfo-div").window({title: '添加菜单', iconCls:'icon-add', closed: false});
}

function exec_add() {
	var data = $("#menuinfo-form").serializeArray();
	$.ajax({
		url: 'system/Menu/add',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if(data.result > 0) {
				$('#menuinfo-form')[0].reset();
				$('#menuinfo-div').window('close');
				alert("添加成功");
				$("#index_menu").tree();
				$(".easyui-simplegrid").simplegrid();
			} else {
				this.error();
			}
		},
		error: function() {
			alert("添加失败");
		}
	});
}

/**
 * 编辑子菜单
 */
function btn_edit(id) {
	var menu = null;
	if (id == 'tree') {
		menu = $("#index_menu").tree("getSelected");
	} else {
		var menu = $(".easyui-simplegrid").simplegrid("getSelected");
		if (menu.length == 0) {
			alert("请选择一个菜单.");
			return;
		}
		menu = menu[0];
	}
	$("#menuinfo-button").unbind();
	$("#menuinfo-button").click(exec_edit);
	$("#menuinfo-form").form("load", {
		'menuInfo.id': menu.id,
		'menuInfo.pid': menu.pid,
		'menuInfo.text': menu.text,
		'menuInfo.href': menu.href,
		'menuInfo.iconCls': menu.iconCls,
		'menuInfo.note': menu.note
	});
	$("#menuiconcls").attr("class", menu.iconCls);
	$("#menuinfo-div").window({title: '修改菜单', iconCls:'icon-edit', closed: false});
}

function exec_edit() {
	var data = $("#menuinfo-form").serializeArray();
	$.ajax({
		url: 'system/Menu/edit',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if(data.result > 0) {
				$('#menuinfo-form')[0].reset();
				$('#menuinfo-div').window('close');
				alert("编辑成功");
				$("#index_menu").tree();
				$(".easyui-simplegrid").simplegrid();
			} else {
				this.error();
			}
		},
		error: function() {
			alert("编辑失败");
		}
	});
}

/**
 * 删除子菜单
 */
function btn_delete(id) {
	var text = "";
	if (id == 'tree') {
		var now = $("#index_menu").tree("getSelected");
		id = now.id;
		text = now.text;
	} else {
		var now = $(".easyui-simplegrid").simplegrid("getSelected");
		if (now.length == 0) {
			alert("没有选择记录.");
			return;
		}
		for ( var i = 0; i < now.length; i++) {
			id = "," +now[i].id;
			text = "," +now[i].text;
		}
		id = id.substring(1);
		text = text.substring(1);
	}
	if (confirm("确认删除[" + text + "]菜单?")) {
		$.ajax({
			url: 'system/Menu/del',
			type: 'post',
			dataType: 'json',
			data: {id: id},
			success: function(data) {
				if (data.result > 0) {
					alert("删除成功");
					$("#index_menu").tree();
					$(".easyui-simplegrid").simplegrid();
				} else {
					this.error();
				}
			},
			error: function() {
				alert("删除失败");
			}
		});
	}
}

/**
 * 菜单上移
 * @param id
 */
function moveUp(id) {
	if (id == 'tree') {
		menu = $("#index_menu").tree("getSelected");
		id = menu.id;
	}
	$.ajax({
		url: 'system/Menu/moveUp',
		type: 'post',
		dataType: 'json',
		data: {id: id},
		success: function(data) {
			if (data.result > 0) {
				$("#index_menu").tree();
				$(".easyui-simplegrid").simplegrid();
			} else {
				this.error();
			}
		},
		error: function() {
			alert("移动失败");
		}
	});
}

/**
 * 菜单下移
 * @param id
 */
function moveDown(id) {
	if (id == 'tree') {
		menu = $("#index_menu").tree("getSelected");
		id = menu.id;
	}
	$.ajax({
		url: 'system/Menu/moveDown',
		type: 'post',
		dataType: 'json',
		data: {id: id},
		success: function(data) {
			if (data.result > 0) {
				$("#index_menu").tree();
				$(".easyui-simplegrid").simplegrid();
			} else {
				this.error();
			}
		},
		error: function() {
			alert("移动失败");
		}
	});
}